﻿package com.ostrich.manpower.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.web.BaseAction;
import com.ostrich.manpower.bean.TostHrAccfundinfo;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.common.OstDateUtils;
import com.ostrich.manpower.service.impl.TostHrAccfundinfoService;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;


/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostHrAccfundinfo")
public class TostHrAccfundinfoController extends BaseAction {
	String jname=null;
	String idCade=null;
	private final static Logger log = Logger
			.getLogger(TostHrAccfundinfoController.class);

	// Servrice start
	@Autowired(required = false)
	// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrAccfundinfoService<TostHrAccfundinfo> tostHrAccfundinfoService;
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice; 
	/**
	 * ilook 首页
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(TostHrAccfundinfo tostHrAccfundinfo,
			HttpServletRequest request) throws Exception {
		Map<String, Object> context = getRootMap();
		// List<TostHrAccfundinfo> dataList =
		// tostHrAccfundinfoService.queryByList(tostHrAccfundinfo);
		// 设置页面数据
		// context.put("dataList", dataList);
		System.out.println("*********");
		return forword("manpower/tabTostHrAccfundinfo", context);
	}
	
	/**
	 * json 列表页面
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(TostHrAccfundinfo tostHrAccfundinfo,
			HttpServletResponse response) throws Exception {
		List<TostHrAccfundinfo> dataList = tostHrAccfundinfoService
				.queryByList(tostHrAccfundinfo);
		
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", tostHrAccfundinfo.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	/**
	 * 添加或修改数据
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */

	
	
	@RequestMapping("/save")
	public void save(TostHrAccfundinfo tostHrAccfundinfo,HttpServletResponse response) throws Exception{
		tostHrAccfundinfo.setHacMonth(OstDateUtils.getDateByMonth(tostHrAccfundinfo.getHacMonth()));
		if(tostHrAccfundinfo.getId() == null){
			tostHrAccfundinfoService.add(tostHrAccfundinfo);
		}else{
			tostHrAccfundinfoService.update(tostHrAccfundinfo);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	
	

	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = getRootMap();
		TostHrAccfundinfo bean = tostHrAccfundinfoService.queryById(id);
		if (bean == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}

	/*
	 * @RequestMapping("/getall") public void getall(TostHrAccfundinfo page,
	 * HttpServletResponse response) throws Exception {
	 * 
	 * List<TostHrAccfundinfo> bean =
	 * tostHrAccfundinfoService.queryByList(page); if (bean == null) {
	 * sendFailureMessage(response, "没有找到对应的记录!"); return; } else { // 设置页面数据
	 * Map<String, Object> jsonMap = new HashMap<String, Object>();
	 * jsonMap.put("total", bean.size()); jsonMap.put("rows", bean);
	 * HtmlUtil.writerJson(response, jsonMap); } }
	 */

	
	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletResponse response)
			throws Exception {
		tostHrAccfundinfoService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public ModelAndView upload(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 得到上传的文件
		MultipartFile mFile = multipartRequest.getFile("excelPath");
		// 得到上传服务器的路径
		String path = request.getSession().getServletContext()
				.getRealPath("/WEB-INF/upload/");
		// 得到上传的文件的文件名
		String filename = mFile.getOriginalFilename();
		InputStream inputStream = mFile.getInputStream();
		byte[] b = new byte[1048576];
		int length = inputStream.read(b);
		path += "\\" + filename;
		// 文件流写到服务器端
		FileOutputStream outputStream = new FileOutputStream(path);
		outputStream.write(b, 0, length);
		inputStream.close();
		outputStream.close();
		request.setAttribute("filepath", path);
		
		//request.setAttribute("path", path);
		request.setAttribute("month", request.getParameter("hacMonth"));
		//return new ModelAndView("forward:/tostHrMealaddinfo/upimport.do");
		
		return new ModelAndView("forward:/tostHrAccfundinfo/upimport.do");
	}
	
	@RequestMapping(value = "/upimport")
	public ModelAndView upimport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> context = getRootMap();
		
		String path = (String)request.getAttribute("filepath");
		String month = (String) request.getAttribute("month");
		
		String times12 = month.substring(0,4);
		String times11 = month.substring(5,7);
		String hs = times12 + times11;
		try {
			InputStream fis = new FileInputStream(path);
			HSSFSheet sh = ImportUtil.getSheet(fis);
			String msg=tostHrAccfundinfoService.setAccfundinfoBySheet(sh,hs);
			msg=(msg!=null&&msg.length()>0)?"但是有异常数据未导入："+msg:"";
			//request.setAttribute("success", "您已经导入成功请查看!");
			request.setAttribute("msg","您已经导入成功请查看!"+ msg);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "导入失败，请查看xls文件是否格式正确");
		}
		request.setAttribute("url", request.getContextPath()
				+ "/tostHrAccfundinfo/list.do");
		return forword("penmsg", context);
	}
	//查出身份证号
	@RequestMapping(value = "/hridcade")
	public void  queryIDcadeByName(HttpServletRequest request,HttpServletResponse response){
	    int id = Integer.parseInt(request.getParameter("hrId"));
	    TostHrBaseinfo baseInfo=tosthrbaseinfoservice.queryById(id);
	    
		HtmlUtil.writerJson(response, baseInfo);
	}
}
